<?php

class openapi_api_function_v1_comment extends openapi_api_function_abstract implements openapi_api_function_interface{

    public function getcomment($params,&$code,&$sub_msg)
    {
        $member_id = $this->checklogin($params['login_token']);
        $filter['type_id'] = $params['goods_id'];
        if($params['append']){
            $filter['comments_type'] = '3';
        }
        $filter['comment|noequal'] = '';
        $filter['orderb'] = '1';
        $member_data = $this->get_current_member();
        $memInfo['member_id'] = $member_data['member_id'];

        $gid = $filter;
        $type='discuss';
        $custom_view="";
        $render = app::get('b2c')->render();
        $objComment = kernel::single('business_message_disask'); 
        $page = empty($params['page'])?1:$params['page'];
        $commentList = $objComment->getGoodsIndexComments($gid,$type,$page);
        if(is_array($gid)) $gid = $gid['type_id']; 
        $aComment[$type] = $commentList['data'];
        // $aComment['page']['start'] = $commentList['start'];
        // $aComment['page']['end'] = $commentList['end'];
        $aComment[$type.'Count'] = $commentList['total'];
        $aComment[$type.'current'] = $commentList['current_page'];
        $aComment[$type.'totalpage'] = $commentList['page'];
        $aComment['discuss_status'] = kernel::single('b2c_message_disask')->toValidate('discuss',$gid,$memInfo,$discuss_message);
        $aComment['discuss_message'] = empty($member_id)?"nologin":"";
        //获取提示文字 @lujy
        // $aComment['setting'] = $objComment->get_setting($type);

        for($i=0;$i<$commentList['page'];$i++){
            $aComment[$type.'Page'][] = $i;
        }
        $aId = array();
        if ($commentList['total']){
            foreach($aComment[$type] as $rows){
                $aId[] = $rows['comment_id'];
            }
            if(count($aId)){
                $addition = array();
                $temp = app::get('b2c')->model('member_comments')->getList('comment_id,for_comment_id',array('for_comment_id' => $aId,'comments_type'=>'3','display' => 'true'));
                foreach((array)$temp as $rows){
                    $aId[] = $rows['comment_id'];
                    $addition[$rows['for_comment_id']][] = $rows['comment_id'];
                }
            }
            if(count($aId)) $aReply = (array)$objComment->getCommentsReply($aId, true);
            reset($aComment[$type]);
            foreach($aComment[$type] as $key => $rows){
                foreach($aReply as $rkey => $rrows){
                    if($rows['comment_id'] == $rrows['for_comment_id']){
                        $aComment[$type][$key]['items'][] = $aReply[$rkey];
                    }elseif(!empty($addition) && isset($addition[$rows['comment_id']]) && in_array($rrows['for_comment_id'],$addition[$rows['comment_id']])){
                        $aComment[$type][$key]['items'][] = $aReply[$rkey];
                    }
                }
                reset($aReply);
            }
        }
        $aComment['null_notice'][$type] = app::get('b2c')->getConf('comment.null_notice.'.$type);
        return $this->apiData(true,$aComment,200);
    }
}